--[[
   Copyright (c) 2012 TPV R&D Center Brazil
   All rights reserved
 
   FileName: app.audio
 
   Created by: petrina.kimura
   Date: 11/28/2012
--]]
------------------------------------------------------------------------
--local variables

------------------------------------------------------------------------
--module table
--module Audio - responsible of all operations related to audio,
--such as mute audio, audio up or down.
require('basicZapper')

local tAudioApp = {}
local bMuteUser = false
local bMuteSystem = false

local audioOutputList = control.getAudioOutputList()
------------------------------------------------------------------------
--module functions

-- Function to mute audio when mute key is pressed
function tAudioApp.getUserMuteAudio()
   return bMuteUser
end

-- Function to mute audio when mute key is pressed
function tAudioApp.muteAudio()
   bMuteSystem = true
   control.muteAudio(audioOutputList[1])
   control.muteAudio(audioOutputList[#audioOutputList])
   return bMuteSystem
end

-- Function to unmute audio when mute key is pressed
function tAudioApp.unmuteAudio()
   bMuteSystem = false
   if (bMuteUser == false) then
      control.unmuteAudio(audioOutputList[1])
      control.unmuteAudio(audioOutputList[#audioOutputList])
   end
   return bMuteSystem
end

-- User mute

-- Function to mute audio when mute key is pressed
function tAudioApp.muteAudioUser()
   bMuteUser = true
   control.muteAudio(audioOutputList[1])
   control.muteAudio(audioOutputList[#audioOutputList])
   return bMuteUser
end

-- Function to unmute audio when mute key is pressed
function tAudioApp.unmuteAudioUser()
   bMuteUser = false
   if (bMuteSystem == false) then
      control.unmuteAudio(audioOutputList[1])
      control.unmuteAudio(audioOutputList[#audioOutputList])
   end
   return bMuteUser
end

-- Function to increase audio when volume_up key is pressed
function tAudioApp.volumeUp()
   control.volumeUp(audioOutputList[1])
   control.volumeUp(audioOutputList[#audioOutputList])
   return true
end

-- Function to decrease audio when volume_down key is pressed
function tAudioApp.volumeDown()
   control.volumeDown(audioOutputList[1])
   control.volumeDown(audioOutputList[#audioOutputList])
   return true
end

--- Synchronous function to set the volume level.
-- @param volumeLevel number This is the level of the volume to be set.
-- @return boolean True if the command was sent successfully, false if some error occured.
function tAudioApp.setVolume(volumeLevel)
   control.setVolume(audioOutputList[1], volumeLevel)
   control.setVolume(audioOutputList[#audioOutputList], volumeLevel)
   return true
end

--- Synchronous function to get the volume level.
-- @return number The current volume level.
function tAudioApp.getVolume()
	return control.getVolume(control.getCurrentAudio())
end

--- Synchronous function to get the list of audios available
-- @return list
function tAudioApp.getAudioList()
   local tAudioList = control.getAudioList()
   return tAudioList
end

--- Synchronous function to set the audio selected
-- @param audio number. this is the audio of the to be set.
function tAudioApp.setAudio(audio)
	return control.setAudio(audio)
end

--- Synchronous function to get the current audio
-- @return number
function tAudioApp.getAudio()
	return control.getCurrentAudio()
end

------------------------------------------------------------------------
return tAudioApp
